const express = require("express");
const app = express();
const port = 3000;

const Mock = require("mockjs");

//设置跨域访问
app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1')
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

app.get("/api/userList", (req, res) => {
  const queryParams = req.query;
  let data;
  Mock.mock(function () {
    const list = Mock.mock({
      "list|10": [
        {
          "id|+1": 1,
          name: "@cname",
          "age|18-25": 18,
          "sex|1": ["男", "女"],
          "job|1": ["php", "ui", "web", "java"],
        },
      ],
    });
    if (queryParams.id) {
      data = list.list.filter((item) => item.id == queryParams.id);
    } else {
      data = list.list;
    }
  });
  res.send(data);
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});
